package botcraft.game.xml;

import java.util.Collections;
import java.util.Iterator;

import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;

import botcraft.game.Game;

public class BotcraftNamespaceContext implements NamespaceContext {
	@Override
	public String getNamespaceURI(final String prefix) {
		if (prefix == null)
			throw new IllegalArgumentException("Prefix can not be null");
		else if (prefix.equals(XMLConstants.XML_NS_PREFIX))
			return XMLConstants.XML_NS_URI;
		else if (prefix.equals(XMLConstants.XMLNS_ATTRIBUTE))
			return XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
		else if (prefix.equals(Game.PREFIX))
			return Game.NAMESPACE.toString();
		else // default prefix or unbound prefix
			return XMLConstants.NULL_NS_URI;
	}
	
	@Override
	public String getPrefix(final String namespaceUri) {
		if (namespaceUri == null)
			throw new IllegalArgumentException("Namespace URI can not be null");
		else if (namespaceUri.equals(XMLConstants.XML_NS_URI))
			return XMLConstants.XML_NS_PREFIX;
		else if (namespaceUri.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI))
			return XMLConstants.XMLNS_ATTRIBUTE;
		else if (namespaceUri.equals(Game.NAMESPACE.toString()))
			return Game.PREFIX;
		else
			return null;
	}
	
	@Override
	public Iterator<String> getPrefixes(final String namespaceUri) {
		return Collections.singleton(getPrefix(namespaceUri)).iterator();
	}	
}
